package com.ht.modules.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.modules.device.entity.DeviceElectricityHour;
import com.ht.modules.device.entity.DeviceElectricityHourVo;
import com.ht.modules.device.entity.DeviceElectricityStatisticVo;
import com.ht.modules.device.mapper.DeviceElectricityHourMapper;
import com.ht.modules.device.service.IDeviceElectricityHourService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 *  电表小时数据
 * @Author:
 * @Date: 2025-03-25
 * @Version: V1.0
 */
@Service
public class DeviceElectricityHourServiceImpl extends ServiceImpl<DeviceElectricityHourMapper, DeviceElectricityHour> implements IDeviceElectricityHourService {

    @Autowired
    private DeviceElectricityHourMapper deviceElectricityHourMapper;

    /**
     * 分页列表查询
     *
     * @param deviceElectricityHourVo 参数
     * @param page                    分页对象
     * @param column                  默认排序列
     * @param order                   默认排序方式
     * @return 分页列表
     */
    @Override
    public Page<DeviceElectricityHourVo> list(Page<DeviceElectricityHourVo> page, DeviceElectricityHourVo deviceElectricityHourVo, String column, String order) {
        return page.setRecords(deviceElectricityHourMapper.list(page, deviceElectricityHourVo, column, order));
    }

    /**
     * 不分页列表查询
     *
     * @param deviceElectricityHourVo 参数
     * @param column                  默认排序列
     * @param order                   默认排序方式
     * @return 不分页列表
     */
    @Override
    public List<DeviceElectricityHourVo> listNoPage(DeviceElectricityHourVo deviceElectricityHourVo, String column, String order) {
        return deviceElectricityHourMapper.list(null, deviceElectricityHourVo, column, order);
    }

    /**
     * 添加
     *
     * @param deviceElectricityHour 参数
     */
    @Override
    public void add(DeviceElectricityHour deviceElectricityHour) {
        this.save(deviceElectricityHour);
    }

    /**
     * 编辑
     *
     * @param deviceElectricityHour 参数
     */
    @Override
    public void edit(DeviceElectricityHour deviceElectricityHour) {
        DeviceElectricityHour deviceElectricityHourEntity = this.getById(deviceElectricityHour.getId());
        if (null == deviceElectricityHourEntity) {
            throw new RuntimeException("未找到对应实体！");
        }
        this.updateById(deviceElectricityHour);
    }

    /**
     * 通过id删除（逻辑删除）
     *
     * @param id 参数
     */
    @Override
    public void delete(Integer id) {
        DeviceElectricityHour deviceElectricityHour = this.getById(id);
        if (null == deviceElectricityHour) {
            throw new RuntimeException("未找到对应实体！");
        }
        boolean b = this.removeById(id);
        if (!b) {
            throw new RuntimeException("删除失败！");
        }
    }

    /**
     * 批量删除（逻辑删除）
     *
     * @param ids 参数
     */
    @Override
    public void deleteBatch(String ids) {
        if (Strings.isBlank(ids)) {
            throw new RuntimeException("参数不识别！");
        }
        deviceElectricityHourMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
    }

    /**
     * 通过id查询
     *
     * @param id 参数
     * @return 查询结果
     */
    @Override
    public DeviceElectricityHour queryById(Integer id) {
        DeviceElectricityHour deviceElectricityHour = this.getById(id);
        if (null == deviceElectricityHour) {
            throw new RuntimeException("未找到对应实体！");
        }
        return deviceElectricityHour;
    }

    /**
     * 通过id查询Vo
     *
     * @param id 参数
     * @return 查询结果
     */
    @Override
    public DeviceElectricityHourVo queryVoById(Integer id) {
        DeviceElectricityHourVo deviceElectricityHourVo = deviceElectricityHourMapper.getVoById(id);
        if (null == deviceElectricityHourVo) {
            throw new RuntimeException("未找到对应实体！");
        }
        return deviceElectricityHourVo;
    }

    @Override
    public List<DeviceElectricityStatisticVo> queryElectricityMeterDayList(DeviceElectricityStatisticVo deviceElectricityStatisticVo) {
        return deviceElectricityHourMapper.queryElectricityMeterDayList(deviceElectricityStatisticVo);
    }

    @Override
    public List<DeviceElectricityStatisticVo> queryElectricityMeterHourList(DeviceElectricityStatisticVo deviceElectricityStatisticVo) {
        return deviceElectricityHourMapper.queryElectricityMeterHourList(deviceElectricityStatisticVo);
    }

    @Override
    public List<DeviceElectricityHour> electricalEquipmentAnalysis(String id, String startTime, String endTime) {
        return this.list(new LambdaQueryWrapper<DeviceElectricityHour>()
                .eq(DeviceElectricityHour::getDeviceId, id)
                .ge(DeviceElectricityHour::getDateTime, startTime)
                .le(DeviceElectricityHour::getDateTime, endTime)
        );
    }
}
